<!-- File: /app/views/posts/search.ctp -->

<?php
echo $this->Html->script('search', false);
echo $this->element('bbcode');
// echo $flag."<br/>";
// print_r($search1);
//echo $this->Form->create('Post', array('action' => 'search', 'id'=>'searchBar', 'type'=> 'get'));
////echo $this->Form->input('Comment.reply', array('type' => 'hidden', 'value' => '0'));
//////echo $this->Form->input('Post.id', array('type' => 'hidden', 'value' => $post['Post']['id']));
////echo "<a href='javascript:void(0)' class='moreless' onclick='showhide(this)' >[Options]</a><span class='name' style='display:none;'>";
////echo $this->Form->input('Comment.name', array('class'=>'name1'));
////echo $this->Form->input('Comment.email', array('class'=>'name1'));
////echo "</span>";
////echo $this->Form->input('Comment.text', array('class'=>'texting', 'id'=>'textual'));
////echo $this->Form->button('Add comment', array('type'=>'button','onclick'=>'return comment(this)', 'class'=>'cancel'));
//$options = array('M' => 'Recent', 'R' => 'Random', 'F' => 'Top');
//echo $this->Form->input('searchstring', array('type' => 'hidden', 'value' => $search2));
//echo $this->Form->select('sort', $options);
//echo $this->Form->submit('Filter', array());
// echo $this->Form->end(); 
if (isset($advanced))
    echo "<div id='advanced' style='width:70%;'>";
else
    echo "<div id='advanced' style='width:100%'>";
echo "<span id='search1' style='display:none;'>" . $search2 . "</span><br/>";
$search3 = explode(" ", $search2);
if (count($search3) == 1)
    echo "Are you looking for the <a href='/urp/tags/view/" . $search2 . "'>" . $search2 . "</a> wiki page?<br/><br/>";


if (count($search) == 0) {
    echo "No results.";
    return;
}

$x = 1;


if ($viewas == 1) {
    echo '<table class="table3">
	<tr>
		<th>Name</th>
		<th>Title</th>
                <th>Text</th>
                <th>Tags</th>
		
	</tr>';

    foreach ($search as $sea) {
        $x++;
        echo "<tr><td>";
        if (isset($sea['Post']))
            echo "<a href='/urp/posts/view/" . $sea['Post']['id'] . "' class='postTitle1'>" . $sea['Post']['title'] . "</a> by <a href='' class='authorName'>" . $sea['Post']['name'] . "</a>";
        else
            echo "<a href='/urp/posts/view/" . $sea['Comment']['post_id'] . "#" . $sea['Comment']['id'] . "' class='authorName'>" . $sea['Comment']['name'] . "</a>";
        echo "</td><td>";
        if (isset($sea['Post']))
            echo $sea['Post']['created'];
        else
            echo $sea['Comment']['created'];
        echo "</td><td class='tagthem'>";

        if (count($sea['Tag']) > 0) {
            $tags1 = "";
            foreach ($sea['Tag'] as $tag) {
                if (strpos($tag['tag'], "board:") !== false)
                    echo "<div><a class='boardLink' href='?search=" . $tag['tag'] . "'>" . $tag['tag'] . "</a></div>, ";
                else if (strpos($tag['tag'], "test:") !== false)
                    echo "<div><a class='boardLink1' href='?search=" . $tag['tag'] . "'>" . $tag['tag'] . "</a></div>, ";
                else
                    echo "<div><a href='?search=" . $tag['tag'] . "' >" . $tag['tag'] . "</a></div>, ";
                $tags1.=$tag['tag'] . " ";
            }
            echo "</td><td>";
        }
        if (isset($sea['Post'])) {
            if (strlen($sea['Post']['body']) < 800)
                echo nl2br($sea['Post']['body']);
            else
                echo substr($sea['Post']['body'], 0, 800) . " ... <a href='view/" . $sea['Post']['id'] . "'>More...</a>";
        }
        else {
            if (strlen($sea['Comment']['text']) < 800)
                echo nl2br($sea['Comment']['text']);
            else
                echo nl2br(substr($sea['Comment']['text'], 0, 800)) . " ... <a href='view/" . $sea['Comment']['id'] . "'>More...</a>";
        }
        echo "</td></tr>";
    }
    echo "</table>";
}
else {
    //    echo "</small></p><p>";
    foreach ($search as $sea) {
        
        echo "<div><div class='post3'>";
        echo "<span class='listNum'>" . $x . "</span> "; 
        $x++;
       if (count($sea['Tag']) > 0) {
           echo '<div class="tags1"><b>Tags:</b><span class="tags2">' ;
            $tags1 = "";
            foreach ($sea['Tag'] as $tag) {
                if (strpos($tag['tag'], "board:") !== false)
                    echo "<div><a class='boardLink' href='?search=" . $tag['tag'] . "'>" . $tag['tag'] . "</a></div>, ";
                else if (strpos($tag['tag'], "test:") !== false)
                    echo "<div><a class='boardLink1' href='?search=" . $tag['tag'] . "'>" . $tag['tag'] . "</a></div>, ";
                else
                    echo "<div><a href='?search=" . $tag['tag'] . "' >" . $tag['tag'] . "</a></div>, ";
                $tags1.=$tag['tag'] . " ";
            }
               echo '</span></div>';
        }
//        $this->element('parse_text', array('com'=>  $com1['Comment']['text']));
        if (isset($sea['Post']))
            echo "<a href='/urp/posts/view/" . $sea['Post']['id'] . "' class='postTitle1'>" . $sea['Post']['title'] . "</a> by <a href='' class='authorName'>" . $sea['Post']['name'] . "</a>";
        else
            echo "<a href='/urp/posts/view/" . $sea['Comment']['post_id'] . "#" . $sea['Comment']['id'] . "' class='authorName'>" . $sea['Comment']['name'] . "</a>";

        echo "<p>";
        if (isset($sea['Post'])) {
            $parsedText= bb_code($sea['Post']['body']);
//            $this->element('parse_text', array('com'=> ($sea['Post']['body'])));
            if (strlen( $parsedText) < 800)
                echo  $parsedText;
            else
                echo substr($parsedText, 0, 800) . " ... <a href='view/" . $sea['Post']['id'] . "'>More...</a>";
        }
        else {
            $parsedText= bb_code($sea['Comment']['text']);
//            $this->element('parse_text', array('com'=> ($sea['Comment']['text'])));
            if (strlen( $parsedText) < 800)
                echo  $parsedText;
            else
                echo substr($parsedText, 0, 800) . " ... <a href='view/" . $sea['Comment']['id'] . "'>More...</a>";         
        }
        echo "</p>";

     
        if (isset($sea['Comment'])) {
            echo "<div class='replylinks'>";
            echo "<a href='' onclick='return showChildComments(" . $sea['Comment']['id'] . ", this)'>Show Children</a>";
            echo "</div>";
        }
        echo '</div></div>';
    }
}
echo "</div>";
?>